<?php
require_once "common.php";
require_once "db.php";
require_once "TopSdk.php";

$error = 0;

$cats = r($GLOBALS['db'],
    "select cat_id from goods_cat order by cat_id"
);
$cc = count($cats);

$p = file_get_contents('p');
$p = $p=='' ? array('1', $cats[0]['cat_id']) : explode(':', $p);
$cp = 0;
foreach($cats as $k=>$v) {
    if($v['cat_id']==$p[1]) { $cp = $k; break; }
}
//var_dump($p);exit;

$c = new TopClient;
$c->appkey = '123456';
$c->secretKey = '123456';
$req = new TaobaokeItemsGetRequest;
$req->setFields("num_iid,title,nick,pic_url,price,click_url,commission,commission_rate,commission_num,commission_volume,shop_click_url,seller_credit_score,item_location,volume");
$req->setNick("nick");
$req->setPid("123");
$req->setSort("commissionVolume_desc");
$req->setKeyword("");

$c2 = new TopClient;
$c2->appkey = '123';
$c2->secretKey = '123';
$req2 = new ItemGetRequest;
$req2->setFields("desc,pic_url,num,cid");

while(1) {
    if($error > 10) { l('error: '.$error.' stop', -1); exit; }
    $req->setCid((float)$p[1]);
    $req->setPageNo((int)$p[0]);
    $resp = $c->execute($req);
    if($resp && method_exists($resp, 'count') && 2 == $resp->count()) {
        foreach($resp->children() as $child) {
            if($child->getName()=='taobaoke_items') {
                if($child->count()) {
                    foreach($child->children() as $item) {
                        l('--------------------------------  '.$p[1].'-'.$p[0].'  --------------------------------', 1);
                        if($item->commission<0.1) {
				l('too cheap!', -1);
				continue;
			}
                        l('click_url: '.$item->click_url, 1);
                        l('commission: '.$item->commission, 1);
                        l('item_location: '.$item->item_location, 1);
                        l('num_iid: '.$item->num_iid, 1);
                        l('pic_url: '.$item->pic_url, 1);
                        l('price: '.$item->price, 1);
                        l('title: '.$item->title, 1);
                        l('nick: '.$item->nick, 1);
                        $has = r($GLOBALS['db'],
                            "select count(1) as c from goods where id='".$item->num_iid."'"
                        );
                        if($has[0]['c']) {
                            l('exists', 1);
                            continue;
                        }
                        $req2->setNumIid(floatval($item->num_iid));
                        $resp2 = $c2->execute($req2);
			if($resp2 && method_exists($resp2, 'count') && $resp2->item->num) {
				l('num: '.$resp2->item->num, 1);
				if (!get_magic_quotes_gpc()) {
				    $resp2->item->title = addslashes($resp2->item->title);
				    $resp2->item->desc = addslashes($resp2->item->desc);
				}
			} else ++$error;
                        sleep(10);
                    }
                }
            }
        }
    } else {
	 if(empty($resp)) l('no items', -1); else ++$error;
    }
    if(++$cp >= $cc) { $cp = 0; $p[0] = $p[0] > 98 ? 1 : ++$p[0]; }
    $p[1] = $cats[$cp]['cat_id'];
    file_put_contents('p', implode(':', $p));
    sleep(1);
}
?>
